package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import org.agony2d.debug.Stats;
	import org.agony2d.utils.gc;
	
	import assets.publicAA.PublicAA_Assets;
	
	import org.agony2d.Agony;
	import org.agony2d.gpu.AAFacade;
	import org.agony2d.gpu.core.WorldAA;
	import org.agony2d.input.KeyboardManager;
	import org.agony2d.input.TouchManager;
	import org.agony2d.notify.AEvent;
	
	import stateAAs.animator.*;
	import stateAAs.textures.*;
	import stateAAs.performance.*;
	import stateAAs.fusions.*;
	import stateAAs.*;
	
	[SWF(width = "960", height = "600", frameRate = "30", backgroundColor = "0xFFFFFF")]
public class MobileAACases extends Sprite {
	
	public function MobileAACases() {
		this.addEventListener(Event.ADDED_TO_STAGE, doInit)
	}
	
	private function doInit( e:Event ) : void {
		Agony.startup(stage)
		KeyboardManager.getInstance().initialize()
		AAFacade.startup(0xFFFFFF, false, 0)
		AAFacade.addEventListener(AEvent.BEGINNING, onContextBeginning)
		AAFacade.addEventListener(AEvent.RESET, onContextReset)
//		AAFacade.enableErrorChecking = true
		
		stage.addChild(new Stats(80))
		
		TouchManager.getInstance().multiTouchEnabled = true
	}
	
	
	private var mWorld:WorldAA
	
	
	private function onContextBeginning(e:AEvent):void {
		AAFacade.removeEventListener(AEvent.BEGINNING, onContextBeginning)
		
		trace('beginning...')
		this.doAddTextures()
		this.doAddAtlas()
		this.doAddSheets()
		/////////////////////////////////////////////////////////////////
		/////////////////////////////////////////////////////////////////
		/////////////////////////////////////////////////////////////////
		
		//mWorld = AAFacade.createWorld(-1, -1)
		//mWorld.x = 1
		//mWorld.createLayer(Image_StateAA).stateArgs = [2000]
		//mWorld.createLayer(Image_StateAA).stateArgs = [2000]
		//mWorld.createLayer(Image_StateAA).stateArgs = [2000]
		//mWorld.reset()
		
		mWorld = AAFacade.createWorld( -1, -1)
//		mWorld.x = 100
//		mWorld.createLayer(Benchmark_StateAA)
		//mWorld.createLayer(Batch_StateAA).stateArgs = [3000]
		//mWorld.createLayer(Fusion_StateAA)
		//mWorld.createLayer(Fusion_StateAA_2)
//		mWorld.createLayer(ManyAnimator_StateAA).stateArgs = [1000]
		mWorld.createLayer(ManyAnimator_StateAA).stateArgs = [1000]
//		mWorld.createLayer(Animator_StateAA)
		mWorld.createLayer(Interact_StateAA)
		mWorld.createLayer(Atlas_StateAA)
		mWorld.reset()
		
		//mWorld = AAFacade.createWorld(-1, -1)
		//mWorld.x = 200
		//mWorld.createLayer(TintedImage_StateAA).stateArgs = [300]
		//mWorld.createLayer(TintedImage_StateAA).stateArgs = [300]
		//mWorld.createLayer(TintedImage_StateAA).stateArgs = [300]
		//mWorld.reset()
		
		
		KeyboardManager.getInstance().getState().press.addEventListener("R", function(e:AEvent):void {
			mWorld.reset()
		})
		
		KeyboardManager.getInstance().getState().press.addEventListener("G", function(e:AEvent):void {
			gc()
		})
	}
	
	private function onContextReset(e:AEvent):void{
		this.doAddTextures()
	}
	
	private function doAddTextures() : void{
		AAFacade.addFullTexture("A_32", (new PublicAA_Assets.img_flash_icon_32).bitmapData)
		AAFacade.addFullTexture("A_64", (new PublicAA_Assets.img_flash_icon_64).bitmapData)
		AAFacade.addFullTexture("B", (new PublicAA_Assets.img_role).bitmapData)
		AAFacade.addFullTexture("bgA", (new PublicAA_Assets.img_bg).bitmapData)
		AAFacade.addFullTexture("quad", (new PublicAA_Assets.img_quad).bitmapData)
		
		AAFacade.addFullTexture("atlas", (new PublicAA_Assets.img_atlas).bitmapData)
	}
	
	private function doAddAtlas() : void {
		AAFacade.addAtlas("atlas", XML(new PublicAA_Assets.xml_atlas))
		AAFacade.addRegionTexture("A_32", "half", new Rectangle(8,8,16,16))
		AAFacade.addRegionTexture("A_64", "half", new Rectangle(16,16,32,32))
	}
	
	private function doAddSheets() : void {
		AAFacade.addSheetsByXml(XML(new PublicAA_Assets.xml_sheet))
	}
}
}